# settings_applier.py
import tkinter as tk
from tkinter import ttk

def apply_settings(window, settings):
    """
    应用设置到窗口
    
    参数:
        window: 要应用设置的窗口
        settings: 包含设置的字典，包含以下键:
            - transparency: 透明度 (0.0-1.0)
            - always_on_top: 是否置顶 (True/False)
            - background_color: 背景颜色
            - foreground_color: 前景颜色 (控件背景)
            - text_color: 文本颜色
    """
    # 应用透明度设置
    transparency = float(settings.get('transparency', '1.0'))
    window.attributes('-alpha', transparency)
    
    # 应用置顶设置
    always_on_top = settings.get('always_on_top', 'False').lower() == 'true'
    window.attributes('-topmost', always_on_top)
    
    # 应用背景颜色设置
    bg_color = settings.get('background_color', '#f0f0f0')
    try:
        window.configure(background=bg_color)
    except:
        pass
    
    # 应用前景颜色和文本颜色设置
    fg_color = settings.get('foreground_color', '#f0f0f0')
    text_color = settings.get('text_color', '#000000')
    
    # 配置 ttk 样式
    configure_ttk_styles(fg_color, text_color)
    
    # 尝试应用颜色到所有控件
    try:
        apply_colors_to_widgets(window, fg_color, text_color)
    except Exception as e:
        print(f"应用颜色时出错: {e}")

def configure_ttk_styles(fg_color, text_color):
    """配置 ttk 控件的样式"""
    style = ttk.Style()
    
    # 配置按钮样式
    style.configure('TButton', 
                   background=fg_color, 
                   foreground=text_color)
    
    # 配置标签样式
    style.configure('TLabel', 
                   background=fg_color, 
                   foreground=text_color)
    
    # 配置框架样式
    style.configure('TFrame', 
                   background=fg_color)
    
    # 配置标签框架样式
    style.configure('TLabelframe', 
                   background=fg_color)
    style.configure('TLabelframe.Label', 
                   background=fg_color, 
                   foreground=text_color)
    
    # 配置输入框样式
    style.configure('TEntry', 
                   fieldbackground=fg_color, 
                   foreground=text_color)
    
    # 配置滚动条样式
    style.configure('TScrollbar', 
                   background=fg_color, 
                   troughcolor=fg_color)
    
    # 配置树状视图样式
    style.configure('Treeview', 
                   background=fg_color, 
                   foreground=text_color, 
                   fieldbackground=fg_color)
    
    # 配置复选框样式
    style.configure('TCheckbutton', 
                   background=fg_color, 
                   foreground=text_color)

def apply_colors_to_widgets(widget, fg_color, text_color):
    """递归应用颜色到所有子控件"""
    try:
        # 设置当前控件的颜色
        if isinstance(widget, tk.Frame):
            widget.configure(background=fg_color)
        elif isinstance(widget, tk.Label):
            widget.configure(background=fg_color, foreground=text_color)
        elif isinstance(widget, tk.Button):
            widget.configure(background=fg_color, foreground=text_color)
        elif isinstance(widget, tk.Entry):
            widget.configure(background=fg_color, foreground=text_color)
        elif isinstance(widget, (tk.Text, tk.Listbox)):
            widget.configure(background=fg_color, foreground=text_color)
        elif isinstance(widget, tk.Scrollbar):
            widget.configure(background=fg_color, troughcolor=fg_color)
        elif isinstance(widget, tk.Canvas):
            widget.configure(background=fg_color)
    except Exception as e:
        print(f"应用颜色到控件时出错: {e}")
    
    # 递归应用到所有子控件
    for child in widget.winfo_children():
        apply_colors_to_widgets(child, fg_color, text_color)